/**
 * WrongSet.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.WrongSet
 * xiaocq Create at 2012-7-17 上午10:23:55
 */
package com.xiaocq.wordsgod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xiaocq.wordsgod.db.WordSetUtil;
import com.xiaocq.wordsgod.model.Control;

/**
 * com.xiaocq.wordsgod.WrongSet
 * @author Mystory
 * Create at 2012-7-17 上午10:23:55
 */
public class WrongSet extends Activity{
	private LinearLayout navigator;
	private Button backToMenu, choiceModel;
	private TextView title;
	
	private ListView wrongSetIndex;
	
	private WordSetUtil wordSetUtil;
	
	private int[] nums;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.wrong_set);
		
		initComponents();
		
		wordSetUtil = new WordSetUtil(WrongSet.this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		wordSetUtil.open();
		nums = wordSetUtil.getWrongWordByUnit();
		wordSetUtil.close();
		
		NumAdapter adapter = new NumAdapter(WrongSet.this, nums);
		wrongSetIndex.setAdapter(adapter);
	}
	
	private void initComponents(){
		navigator = (LinearLayout) findViewById(R.id.wrong_set_navigator);
		backToMenu = (Button) navigator.findViewById(R.id.nav_leftBtn);
		title = (TextView) navigator.findViewById(R.id.nav_middleText);
		choiceModel = (Button) navigator.findViewById(R.id.nav_rightBtn);
		
		wrongSetIndex = (ListView) findViewById(R.id.wrong_set_index);
		
		backToMenu.setText(R.string.backToMenu);
		title.setText(R.string.wrongWord);
		choiceModel.setText(R.string.allWrong);
		
		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		choiceModel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra(Control.TEST_ID, 55);
				intent.setClass(WrongSet.this, WrongSetDetail.class);
				startActivity(intent);
			}
		});
		
		wrongSetIndex.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				if(nums[position] == 0){
					return;
				}
				Intent intent = new Intent();
				intent.putExtra(Control.TEST_ID, position + 1);
				intent.setClass(WrongSet.this, WrongSetDetail.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

}
